'use strict';
var lrSnippet = require('grunt-contrib-livereload/lib/utils').livereloadSnippet;
var mountFolder = function (connect, dir) {
    return connect.static(require('path').resolve(dir));
};

module.exports = function (grunt) {
    var files = require('./files').files;
    // load all grunt tasks
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    // configurable paths
    var yeomanConfig = {
        app: 'app',
        dist: 'app/dist'
    };

    try {
        yeomanConfig.app = require('./component.json').appPath || yeomanConfig.app;
    } catch (e) {
    }

    grunt.initConfig({
        buildtag: '-dev-' + grunt.template.today('yyyy-mm-dd'),
        yeoman: yeomanConfig,
        watch: {
            coffee: {
                files: ['<%= yeoman.app %>/scripts/{,*/}*.coffee'],
                tasks: ['coffee:dist']
            },
            coffeeTest: {
                files: ['test/spec/{,*/}*.coffee'],
                tasks: ['coffee:test']
            },
            compass: {
                files: ['<%= yeoman.app %>/static/{,*/}*.{scss,sass}'],
                tasks: ['compass']
            },
            livereload: {
                files: [
                    files.src,
                    files.templates,
                    files.img()
                ],
                tasks: ['livereload']
            }
        },
        connect: {
            options: {
                port: 9000,
                // Change this to '0.0.0.0' to access the server from outside.
                hostname: 'localhost'
            },
            livereload: {
                options: {
                    middleware: function (connect) {
                        return [
                            lrSnippet,
                            mountFolder(connect, '.tmp'),
                            mountFolder(connect, yeomanConfig.app)
                        ];
                    }
                }
            },
            test: {
                options: {
                    middleware: function (connect) {
                        return [
                            mountFolder(connect, '.tmp'),
                            mountFolder(connect, 'test')
                        ];
                    }
                }
            },
            e2e: {
                options: {
                    middleware: function (connect) {
                        return [
                            mountFolder(connect, '.tmp'),
                            mountFolder(connect, 'test'),
                            mountFolder(connect, 'app')
                        ];
                    }
                }
            }
        },
        open: {
            server: {
                url: 'http://localhost:<%= connect.options.port %>'
            }
        },
        clean: {
            dist: {
                files: [
                    {
                        dot: true,
                        src: [
                            '.tmp',
                            '<%= yeoman.dist %>/*'
                        ]
                    }
                ]
            },
            server: '.tmp'
        },
        jshint: {
            options: {
                jshintrc: '.jshintrc'
            },
            all: [
                'Gruntfile.js',
                '<%= yeoman.app %>/static/js/{,*/}*.js'
            ]
        },
        karma: {
            unit: {
                configFile: '<%= yeoman.app %>/config/karma.conf.js',
                singleRun: true
            },
            e2e: {
                configFile: '<%= yeoman.app %>/config/karma-e2e.conf.js',
                singleRun: false,
                autoWatch: true,
                proxies: {
                    '/': 'http://localhost:8000/'
                },
                urlRoot : '/__e2e/'
            }
        },


        concat: {
            dist: {
                files: {
                    '<%= yeoman.dist %>/assets/js/scripts.js': [
//                        'app/static/lib/bootstrap/3.0.3/js/bootstrap.js'
                        files.jquery('1.9.1').concat(files.angular('1.2.9')).concat(files.src)
                    ]
                }
            }
        },
        useminPrepare: {
            html: files.templates,
            options: {
                dest: '<%= yeoman.dist %>'
            }
        },
        usemin: {
            html: ['<%= yeoman.dist %>/templates/{,*/}*.html'],
            css: ['<%= yeoman.dist %>/assets/css/{,*/}*.css'],
            options: {
                dirs: ['<%= yeoman.dist %>']
            }
        },
        imagemin: {
            dist: {
                files: [
                    {
                        expand: true,
                        cwd: '<%= yeoman.dist %>/'+files.imgSrc,
                        src: '{,*/}*.{png,jpg,jpeg}',
                        dest: '<%= yeoman.dist %>/static/img'
                    }
                ]
            }
        },


        cssmin: {
            dist: {
                files: {
                    '<%= yeoman.dist %>/assets/css/main.css': [
                        '.tmp/css/{,*/}*.css',
                        files.css
                    ]
                }
            }
        },

        //Compile LESS files to CSS.
        less:{
            development: {
                options: {
                    paths: ["assets/css"]
                },
                files: {
                    "path/to/result.css": "path/to/source.less"
                }
            }
        },
        htmlmin: {
            dist: {
                options: {
                    collapseBooleanAttributes: true,
                    removeAttributeQuotes: true,
                    removeRedundantAttributes: true,
                    removeEmptyAttributes: true
                    /*removeCommentsFromCDATA: true,
                     // https://github.com/yeoman/grunt-usemin/issues/44
                     collapseBooleanAttributes: true,
                     removeAttributeQuotes: true,
                     removeRedundantAttributes: true,
                     useShortDoctype: true,
                     collapseWhitespace: true,
                     removeEmptyAttributes: true,
                     removeOptionalTags: true*/
                },
                files: [{
                    expand: true,
                    cwd: '<%= yeoman.app %>',
                    src: ['templates/*.html'],
                    dest: '<%= yeoman.dist %>',
                    ext:'.min.html'
                }]
            },
            deploy: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true
                },
                files: [{
                    expand: true,
                    cwd: '<%= yeoman.dist %>',
                    src: ['templates/*.html'],
                    dest: '<%= yeoman.dist %>'
                }]
            }

        },
        ngtemplates: {
            app: {
                cwd: '<%= yeoman.app %>',
                src: files.view,
                dest: '<%= yeoman.dist %>/templates.js',
                options: {
                    htmlmin: { collapseWhitespace: true},
                    prefix: '/'
                }
            }
        },
        cdnify: {
            dist: {
                html: ['<%= yeoman.dist %>/*.html']
            }
        },
        ngmin: {
            dist: {
                files: [
                    {
                        expand: true,
                        cwd: '<%= yeoman.dist %>/js',
                        src: '*.js',
                        dest: '<%= yeoman.dist %>/js'
                    }
                ]
            }
        },
        uglify: {
            dist: {
                files: {
                    '<%= yeoman.dist %>/assets/js/scripts.js': [
                        '<%= yeoman.dist %>/assets/js/scripts.js'
                    ]
                }
            }
        },
        rev: {
            dist: {
                files: {
                    src: [
                        '<%= yeoman.dist %>/assets/js/{,*/}*.js',
                        '<%= yeoman.dist %>/assets/css/{,*/}*.css',
                        '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
                    ]
                }
            }
        },
        copy: {
            dist: {
                files: [
                    {
                        expand: true,
                        dot: true,
                        cwd: '.',
                        dest: '<%= yeoman.dist %>',
                        src: [].concat(files.img('gif,'))
                    },
                    {
                        expand: true,
                        dot: true,
                        cwd: '.',
                        flatten: true,
                        dest: '<%= yeoman.dist %>/assets/fonts',
                        src: [
                            'app/static/lib/bootstrap/3.0.3/fonts/*'
                        ].concat(files.img('gif,'))
                    }
                ]
            }
        }
    });

    grunt.renameTask('regarde', 'watch');


    grunt.registerTask('unit', [
        //'connect:test',
        'karma:unit'
    ]);

    grunt.registerTask('e2e', [
        'connect:e2e',
        'karma:e2e'
    ]);

    grunt.registerTask('build', [
        //清理旧文件
        'clean:dist',
        //js语法检查
//        'jshint',

        'test',
        'useminPrepare',
        //压缩图片
        'imagemin',
//        //压缩CSS
        'cssmin',
        'htmlmin:dist',
        'ngtemplates',


            //js合并
        'concat',
//        //拷贝
        'copy',
//        //?????
//        'cdnify',
//        //格式化代码
        'ngmin',
        //压缩js
       'uglify',
        //替换css js路径
        'rev',
        'usemin',
        //压缩html
        'htmlmin:deploy'
    ]);

    grunt.registerTask('default', ['build']);
};
